package com.swpu.service_archive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_archive.entity.CheckupRecord;
import com.swpu.service_archive.entity.VaccineRecord;
import com.swpu.service_archive.mapper.CheckupRecordMapper;
import com.swpu.service_archive.service.CheckupRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-03-20
 */
@Service
public class CheckupRecordServiceImpl extends ServiceImpl<CheckupRecordMapper, CheckupRecord> implements CheckupRecordService {

    @Override
    public void saveUserCheckupData(String openid, CheckupRecord checkupRecord) {
        checkupRecord.setUserId(openid);
        List<String> pictureUrls = checkupRecord.getPictureUrls();
        String pictures = "";

        for (String pictureUrl : pictureUrls) {
            pictures+=pictureUrl+"&";
        }
        if(pictures.length()>0)
        pictures= pictures.substring(0,pictures.length()-1);
        checkupRecord.setPictures(pictures);
        boolean isSave = this.save(checkupRecord);
        if(!isSave){
            throw new RuntimeException("用户疫苗接种数据保存失败");
        }
    }

    @Override
    public void updateUserCheckupData(CheckupRecord checkupRecord) {
        List<String> pictureUrls = checkupRecord.getPictureUrls();
        String pictures = "";
        if(pictureUrls.size()>0){
            for (String pictureUrl : pictureUrls) {
                pictures+=pictureUrl+"&";
            }
            pictures= pictures.substring(0,pictures.length()-1);
        }
        checkupRecord.setPictures(pictures);
        this.updateById(checkupRecord);
    }

    @Override
    public CheckupRecord getCheckupDataById(String id) {
        CheckupRecord checkupRecord = this.getById(id);
        return checkupRecord;
    }

    @Override
    public List<CheckupRecord> getUserCheckupList(String openid) {
        LambdaQueryWrapper<CheckupRecord> wrapper = new LambdaQueryWrapper<>();
        List<CheckupRecord> checkupList = this.list(wrapper);
        return checkupList;
    }

    @Override
    public int getCheckupRecordCount(String openid) {
        LambdaQueryWrapper<CheckupRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckupRecord::getUserId,openid);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public void deleteCheckupById(String id) {
        this.removeById(id);
    }
}
